package com.njbw.entity;

import com.njbw.constant.ResultCode;
import com.njbw.constant.ResultMsg;
import lombok.Data;

import java.io.Serializable;

/**
 * 目的是提供一些可以直接使用的方法
 */
@Data
public class Result<T> implements Serializable {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    /**
     * 不让使用者自己 new，而是只能使用我们提供的 API
     */
    private Result() {

    }

    public static Result<?> ok() {
        Result<?> result = new Result<>();
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMsg.SUCCESS);
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMsg.SUCCESS);
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        return result;
    }

    public static Result<?> ok(Integer resultCode, String message) {
        Result<?> result = new Result<>();
        result.setCode(resultCode);
        result.setMessage(message);
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    public static <T> Result<T> ok(Integer resultCode, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(resultCode);
        result.setMessage(message);
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        return result;
    }

    public static Result<?> fail() {
        Result<?> result = new Result<>();
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMsg.ERROR);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMsg.ERROR);
        result.setSuccess(Boolean.FALSE);
        result.setData(data);
        return result;
    }

    public static Result<?> fail(Integer resultCode, String message) {
        Result<?> result = new Result<>();
        result.setCode(resultCode);
        result.setMessage(message);
        result.setSuccess(Boolean.FALSE);
        return result;
    }

    public static <T> Result<T> fail(Integer resultCode, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(resultCode);
        result.setMessage(message);
        result.setSuccess(Boolean.FALSE);
        result.setData(data);
        return result;
    }
}
